package commonInfrastructure.personalSchedule;


import entity.Event;
import entity.Staff;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.primefaces.model.ScheduleEvent;

@Stateless
public class EventSessionBean implements EventSessionBeanRemote {

    @PersistenceContext
    private EntityManager em;
    private Event event;
    private String title;
    private Staff staff;
   
    @Override
    public String createEvent(ScheduleEvent sEvent, Long staffId) {
        staff = em.find(Staff.class, staffId);
        event = new Event();
        
        event.create(sEvent);
       
        em.persist(event);
        staff.getEvent().add(event);
        em.persist(staff);
        
        em.flush();
        return event.getEventId();
    }
    @Override
    public List<Event> readEvents() {
        
        Query query = em.createQuery("SELECT e FROM Event e ");
        
        return query.getResultList();
        
        
    }
    @Override
    public void updateEvent(ScheduleEvent sEvent, String id){
        
        event = em.find(Event.class, id);
       
        event.setTitle(sEvent.getTitle());
        event.setIsAllDay(sEvent.isAllDay());
        event.setStartDate(String.valueOf(sEvent.getStartDate()));
        event.setEndDate(String.valueOf(sEvent.getEndDate()));
        em.flush();
    }
    @Override
    public void deleteEvent (String id, Long staffID){
        staff = em.find(Staff.class, staffID);
        
        event = em.find(Event.class, id);
        staff.getEvent().remove(event);
        em.remove(event);
        em.flush();
    }
    
    @Override
    public Event getEvent(String id){
        Event e = em.find(Event.class,id);
        return e;
    }
    
}
